package com.example.demo.controller;

import com.example.demo.entity.Goods;
import com.example.demo.entity.Result;
import com.example.demo.entity.Shop;
import com.example.demo.service.GoodsService;
import com.example.demo.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class ShopController {

    @Autowired
    ShopService shopService;

    @CrossOrigin
    @GetMapping(value = "/api/listShop")
    @ResponseBody
    public Result listShops()
    {
        List<Shop> shops = shopService.findShops();
        Result result = new Result(200);
        result.setData(shops);
        return result;
    }

    @CrossOrigin
    @GetMapping(value = "/api/listShopsBySearchMsg")
    @ResponseBody
    public Result listShopsBySearchMsg(@RequestParam("searchMsg") String searchMsg,
                                       @RequestParam("currentPage") int currentPage)
    {
        List<Shop> shops = shopService.listShopsBySearchMsg(searchMsg,currentPage);
        int total = shopService.listShopsBySearchMsgTotal(searchMsg);
        Result result = new Result(200);
        result.setData(shops);
        result.setTotal(total);
        return result;
    }

    @CrossOrigin
    @GetMapping(value = "/api/findShop")
    @ResponseBody
    public Result findShop(@RequestBody Goods goods)
    {
        Shop shop = shopService.findShop(goods);
        Result result = new Result(200);
        result.setData(shop);
        return result;
    }

    @CrossOrigin
    @PostMapping(value = "/api/editShop")
    @ResponseBody
    public void editShop(@RequestBody Shop shop)
    {
        shopService.editShop(shop);
    }

    @CrossOrigin
    @GetMapping(value = "/api/deleteShop")
    @ResponseBody
    public void deleteShop(@RequestParam("id") int id)
    {
        shopService.deleteShopById(id);
    }

    @CrossOrigin
    @PostMapping(value = "/api/addShop")
    @ResponseBody
    public void addShop(@RequestBody Shop shop)
    {
        shopService.addShop(shop);
    }
}
